package com.example.ovulationperiodtracker.view;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 隐私协议TextView（纯代码配置版本）
 * 支持通过方法配置多个协议链接，点击后回调到Activity处理
 */
public class PrivacyPolicyTextView extends AppCompatTextView {

    private List<Protocol> mProtocols = new ArrayList<>();
    private OnProtocolClickListener mListener;
    private int mLinkColor = Color.parseColor("#ff5660"); // 默认蓝色
    private String mMainText = "";

    public PrivacyPolicyTextView(Context context) {
        this(context, null);
    }

    public PrivacyPolicyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PrivacyPolicyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 设置默认属性
        setMovementMethod(LinkMovementMethod.getInstance());
        setHighlightColor(Color.TRANSPARENT); // 点击时不显示背景色
    }

    /**
     * 设置主文本内容
     */
    public PrivacyPolicyTextView setMainText(String text) {
        this.mMainText = text;
        applyProtocolStyle();
        return this;
    }

    /**
     * 设置链接文本颜色
     */
    public PrivacyPolicyTextView setLinkColor(@ColorInt int color) {
        this.mLinkColor = color;
        applyProtocolStyle();
        return this;
    }

    /**
     * 添加协议链接
     */
    public PrivacyPolicyTextView addProtocol(String text, String tag) {
        mProtocols.add(new Protocol(text, tag));
        applyProtocolStyle();
        return this;
    }

    /**
     * 批量添加协议链接
     */
    public PrivacyPolicyTextView addProtocols(List<Protocol> protocols) {
        mProtocols.addAll(protocols);
        applyProtocolStyle();
        return this;
    }

    /**
     * 设置协议点击监听器
     */
    public void setOnProtocolClickListener(OnProtocolClickListener listener) {
        this.mListener = listener;
    }

    private void applyProtocolStyle() {
        if (TextUtils.isEmpty(mMainText)) return;

        SpannableString spannableString = new SpannableString(mMainText);

        for (Protocol protocol : mProtocols) {
            int startIndex = mMainText.indexOf(protocol.text);
            if (startIndex >= 0) {
                int endIndex = startIndex + protocol.text.length();

                // 设置点击样式和事件
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        if (mListener != null) {
                            mListener.onProtocolClick(protocol.tag);
                        }
                    }

                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(mLinkColor);      // 设置链接颜色
                        ds.setUnderlineText(false);   // 取消下划线
                    }
                };

                spannableString.setSpan(clickableSpan, startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        setText(spannableString);
    }

    /**
     * 协议信息类
     */
    public static class Protocol {
        public String text;  // 显示的文本
        public String tag;   // 标识标签

        public Protocol(String text, String tag) {
            this.text = text;
            this.tag = tag;
        }
    }

    /**
     * 协议点击回调接口
     */
    public interface OnProtocolClickListener {
        void onProtocolClick(String tag);
    }
}